package com.ayoka.parenting.ui.grown.index;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.GrownIndexBean;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.interf.SimpleTICCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.jincp.common.utils.UIUtils;
import com.tencent.ticsdk.core.TICManager;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GrownIndexPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GrownIndexPresenter extends BasePresenterImpl<GrownIndexContract.View> implements GrownIndexContract.Presenter {

    private GrownIndexModel model;

    public GrownIndexPresenter() {
        model = new GrownIndexModel();
    }

    @Override
    public void getMyClassRoomList() {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.getMyClassRoomList(this, new SimpleRequestCallback<GrownIndexBean>() {
            @Override
            public void onSuccess(GrownIndexBean data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().configMyClassRoomList(data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
            }
        });
    }

    @Override
    public void createLiveRoom(final int roomId, final String scheduleId) {
        mView.get().showWaitDialog("正在进入房间！");
        TICManager.getInstance().createClassroom(roomId, new SimpleTICCallback() {
            @Override
            public void onSuccess(Object data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
//                    mViewRef.get().showToast("创建课堂 成功, 房间号："  + mRoomId);
                mView.get().onCreateLiveRoomCallback(true, roomId, scheduleId);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                if (errorCode == 6014) {
                    createLiveRoom(roomId, scheduleId);
                } else if (errorCode == 10021) {
//                        mViewRef.get().showToast("该课堂已被他人创建，请\"加入课堂\"");
                    mView.get().onCreateLiveRoomCallback(true, roomId, scheduleId);
                } else if (errorCode == 10025) {
//                        mViewRef.get().showToast("该课堂已创建，请\"加入课堂\"");
                    mView.get().onCreateLiveRoomCallback(true, roomId, scheduleId);
                } else {
                    mView.get().showToast("创建课堂失败, 房间号：" + roomId + " err:" + errorCode + " msg:" + errorMessage);
                    mView.get().onCreateLiveRoomCallback(false, roomId, scheduleId);
                }
            }
        });
    }
}
